package xyz.gits.modules.weread.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import xyz.gits.modules.base.GitsBaseController;
import xyz.gits.modules.weread.entity.UnLimitPeriod;
import xyz.gits.modules.weread.service.IUnLimitPeriodService;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 组队抽取无限卡周期 前端控制器
 * </p>
 *
 * @author 读钓
 * @date 2019-05-13
 */
@Slf4j
@Controller
@RequestMapping("/weread/unLimitPeriod")
public class UnLimitPeriodController extends GitsBaseController {
    private String prefix = "/weread/unLimitPeriod";

    @Autowired
    private IUnLimitPeriodService unLimitPeriodService;

    @RequiresPermissions("weread:unLimitPeriod:view")
    @GetMapping()
    public String unLimitPeriod() {
        return prefix + "/unLimitPeriod";
    }

    @ApiOperation("初始化组队抽取无限卡周期")
    @RequiresPermissions("weread:unLimitPeriod:init")
    @PostMapping(value = "/init")
    @ResponseBody
    public AjaxResult init(Integer periodNum) {
        return toAjax(unLimitPeriodService.init(periodNum));
    }

    @ApiOperation("查询组队抽取无限卡周期列表")
    @RequiresPermissions("weread:unLimitPeriod:list")
    @PostMapping(value = "/list")
    @ResponseBody
    public TableDataInfo list(UnLimitPeriod unLimitPeriod) {
        QueryWrapper<UnLimitPeriod> qw = new QueryWrapper(unLimitPeriod);
        qw.lambda().le(UnLimitPeriod::getEndTime, LocalDateTime.now());
        Page page = getPage(qw);
        IPage result = unLimitPeriodService.page(page, qw);
        return getDataTable(result);
    }

    @ApiOperation("导出组队抽取无限卡周期列表")
    @RequiresPermissions("weread:unLimitPeriod:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UnLimitPeriod unLimitPeriod) {
        List<UnLimitPeriod> list = unLimitPeriodService.list(new QueryWrapper<>(unLimitPeriod));
        ExcelUtil<UnLimitPeriod> util = new ExcelUtil<UnLimitPeriod>(UnLimitPeriod.class);
        return util.exportExcel(list, "unLimitPeriod");
    }

    @ApiOperation("新增组队抽取无限卡周期")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    @ApiOperation("新增保存组队抽取无限卡周期")
    @RequiresPermissions("weread:unLimitPeriod:add")
    @Log(title = "组队抽取无限卡周期", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UnLimitPeriod unLimitPeriod) {
        return toAjax(unLimitPeriodService.save(unLimitPeriod));
    }

    @ApiOperation("修改组队抽取无限卡周期")
    @GetMapping("/edit/{unLimitPeriodId}")
    public String edit(@PathVariable("unLimitPeriodId") String unLimitPeriodId, ModelMap mmap) {
        UnLimitPeriod unLimitPeriod = unLimitPeriodService.getById(unLimitPeriodId);
        mmap.put("unLimitPeriod", unLimitPeriod);
        return prefix + "/edit";
    }

    @ApiOperation("修改保存组队抽取无限卡周期")
    @RequiresPermissions("weread:unLimitPeriod:edit")
    @Log(title = "组队抽取无限卡周期", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UnLimitPeriod unLimitPeriod) {
        return toAjax(unLimitPeriodService.updateById(unLimitPeriod));
    }

    @ApiOperation("删除组队抽取无限卡周期")
    @RequiresPermissions("weread:unLimitPeriod:remove")
    @Log(title = "组队抽取无限卡周期", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(unLimitPeriodService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
